﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;
using GSLMS.Entity.Enum;
using GSLMS.Core;

namespace GSLMS.WebAPP.Areas.System.Controllers.Dist
{
    public class ProductController: AdminBaseController
    {
        protected override string navTabId
        {
            get { return "System_Product"; }
        }

        public ActionResult search(FormCollection form)
        {
            InitSelectList();

            return View(GetProductList(form));
        }

        public ActionResult Index(FormCollection collection)
        {
            InitSelectList();
            return View(GetProductList(collection));
        }

        //[HttpPost]
        //public ActionResult Index(FormCollection collection)
        //{
        //    InitSelectList();

        //    return View(GetProductList(collection));
        //}

        List<GSLMS.Entity.TB_SYS_PRODUCT> GetProductList(FormCollection form)
        {
            var searchParma = new TB_SYS_PRODUCT();
            searchParma = base.SearchPara<TB_SYS_PRODUCT>(form, searchParma);

            //searchParma.PRODUCTNO = Request["PRODUCTNO"];
            //searchParma.PRODUCTNAME = Request["PRODUCTNAME"];
            //searchParma.SYSTEMID = Helper.ToInt(Request["SYSTEMID"]);
            //searchParma.STATUS = Helper.ToInt(Request["STATUS"]);

            //if (ViewData["SearchParma"] != null)
            //{
            //    searchParma = ViewData["SearchParma"] as TB_SYS_PRODUCT;

            //}
            //else
            //{
               
            //}
            var list=serviceSysProduct.GetList(searchParma);
            ViewData["RowCount"] = searchParma.RowCount;
            return list;
        }

       

        public ActionResult Create(long id)
        {

            GSLMS.Entity.TB_SYS_PRODUCT model = new GSLMS.Entity.TB_SYS_PRODUCT();

            if (id > 0)
            {
                model = serviceSysProduct.GetByID(id);
                model.MODIFYBY =CurentUser.UserName;
                model.MODIFYDATE = DateTime.Now;
            }
            else
            {
                model.CREATEBY =CurentUser.UserName;
                model.CREATEDATE = DateTime.Now;
                model.MODIFYBY =CurentUser.UserName;
                model.MODIFYDATE = DateTime.Now;
            }
            
           
            InitSelectList();

            return View(model);
        }

        //
        // POST: /Product/Create

        [HttpPost]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                GSLMS.Entity.TB_SYS_PRODUCT model = new GSLMS.Entity.TB_SYS_PRODUCT();
                TryUpdateModel(model, collection);
                int result =0;
                result = model.ID > 0 ? serviceSysProduct.UpdateProduct(model) : serviceSysProduct.AddProduct(model);
                if(result==1)
                {
                   
                    ajaxStr = string.Format(ajaxStr, 200,
                        ResourceManager.SaveSuccess,
                        navTabId,
                        "",
                         "closeCurrent");
                }
                else if(result==-1)
                {
                    ajaxStr = string.Format(ajaxStr, 300, "产品编码已经存在。", navTabId, "", "");
                }
                else if (result == -2)
                {
                    ajaxStr = string.Format(ajaxStr, 300, "产品名称已经存在。", navTabId, "", "");
                }
                else 
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
                }

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }

        //
        // GET: /Product/Delete/5
        [HttpPost]
        public string Delete(long id)
        {
            try
            {
                if (serviceSysProduct.Delete(id))
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            InitSelectList();

            return "{" + ajaxStr + "}";
            
        }

        private void InitSelectList()
        {

           
            var searchParma = new GSLMS.Entity.TB_SYS_PRODUCT();

           
            if (ViewData["SearchParma"] != null)
            {

                searchParma = ViewData["SearchParma"] as TB_SYS_PRODUCT;
              
            }

            ViewData["SearchParma"] = searchParma ?? new TB_SYS_PRODUCT();
            ViewData["MetInfoSetList"] = new SelectList(serviceSysMetinfoSet.GetMetInfoSetMianList(-1), "ID", "METINFONAME");
            ViewData["salesRptList"] = new SelectList(serviceSysSurveyTmpSet.GetListBySurveyType(-1, 1), "ID", "SURVEYNAME");
            ViewData["riskRptList"] = new SelectList(serviceSysSurveyTmpSet.GetListBySurveyType(-1, 2), "ID", "SURVEYNAME");
            ViewData["IsEffStatus"] = EnumHelper.ToSelectList<SystemEnum.ISEffStatus>();
            ViewData["IntCycleList"] = EnumHelper.ToSelectList<SystemEnum.IntCycle>();
            ViewData["Systemlist"] = EnumHelper.ToSelectList<SystemEnum.SystemID>();
            ViewData["parm1list"] = EnumHelper.ToSelectList<SystemEnum.Parm1>();
            ViewData["parm2list"] = EnumHelper.ToSelectList<SystemEnum.Parm2>();
            

        }


    }
}
